﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PDChart
{

    public class PaletteLegend
    {
        public PointF Location;
        public SizeF Size;

        public Color[] Colors;
        public Color StartColor = Color.Yellow;
        public Color EndColor = Color.Red;
        public int NumberOfColor = 16;

        public Font Font;
        public string MinLabel;
        public string MaxLabel;

        public PaletteLegend(PointF location, SizeF size, Font font, string min, string max)
        {
            this.Location = location;
            this.Size = size;
            this.Font = font;
            this.MinLabel = min;
            this.MaxLabel = max;

            Colors = new Color[NumberOfColor];

            for (int i = 0; i < NumberOfColor; i++)
            {
                Colors[i] = Color.FromArgb((int)((EndColor.ToArgb() - StartColor.ToArgb()) / (NumberOfColor - 1) * i + StartColor.ToArgb()));
            }
        }

        public void Draw(Graphics g)
        {
            for (int i = 0; i < NumberOfColor; i++)
            {
                using (SolidBrush sb = new SolidBrush(Colors[NumberOfColor - i - 1]))
                {
                    g.FillRectangle(sb,
                                Location.X,
                                Location.Y + i * Size.Height / NumberOfColor,
                                Size.Width,
                                Size.Height / NumberOfColor);
                }
            }

            using (Pen pen = new Pen(Color.White))
            {
                for (int i = 0; i <= NumberOfColor; i++)
                {
                    g.DrawLine(pen, Location.X,
                        Location.Y + i * Size.Height / NumberOfColor,
                        Location.X + Size.Width,
                        Location.Y + i * Size.Height / NumberOfColor);
                }

                g.DrawLine(pen, Location.X, Location.Y, Location.X, Location.Y + Size.Height);
                g.DrawLine(pen, Location.X + Size.Width, Location.Y, Location.X + Size.Width, Location.Y + Size.Height);
            }

            using (SolidBrush brush = new SolidBrush(Color.White))
            {
                g.DrawString(MinLabel, this.Font, brush, new PointF(Location.X + Size.Width + 2, Location.Y + Size.Height - this.Font.Height / 2));
                g.DrawString(MaxLabel, this.Font, brush, new PointF(Location.X + Size.Width + 2, Location.Y - this.Font.Height / 2));
            }
        }


        public Color GetColor(double val)
        {
            int idx = 0;

            if (val < 0)
            {
                idx = 0;
            }
            else if (val > 1)
            {
                idx = Colors.Length - 1;
            }
            else
            {
                idx = (int)(val * (NumberOfColor - 1));
            }

            return Colors[idx];

        }
    }
}
